package net.maku.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import net.maku.bean.param.LoginUserParam;
import net.maku.bean.query.WebPayRecordQuery;
import net.maku.bean.vo.WebPayConfigVO;
import net.maku.bean.vo.WebPayRecordVO;
import net.maku.bean.vo.WebTradeRecordVO;
import net.maku.constant.CaffeineKeyConstant;
import net.maku.convert.WebTradeRecordConvert;
import net.maku.entity.WebPayRecordEntity;
import net.maku.entity.WebTradeRecordEntity;
import net.maku.entity.WebUserEntity;
import net.maku.utils.CaffeineUtils;
import net.maku.utils.NewJwtUtils;

@Service
@AllArgsConstructor
public class LoginInfoServiceImpl {
	
    public void saveTrade(WebPayRecordQuery query) {
    	String transactionId ="11111fdsafdasfdsaf111111";
    	String openid = "123456789012345678901234567890";
    	String userid = "12345";
    	Integer status = 1;
    	
    	
    	String  buytype = query.getTradeType();
    	
    	
      	Map<String, WebPayConfigVO> map = (Map<String, WebPayConfigVO>) CaffeineUtils.getCache( CaffeineKeyConstant.PAY_TYPE_KEY_MAP );
    	WebPayConfigVO vo = map.get(query.getTradeType());
    	String amount=vo.getAmount();
    	Integer countnum = vo.getCountnum();
    	
    	
    	WebTradeRecordEntity   tentity = new WebTradeRecordEntity();
    	tentity.setOpenid(openid);
    	tentity.setUserid(userid);
    	tentity.setStatus(status);
    	webTradeRecordServiceImpl.save(tentity);
 
    	WebPayRecordEntity pentity = new WebPayRecordEntity();
    	pentity.setUserid(userid);
    	pentity.setPayerOpenid(openid);
    	pentity.setTransactionId(transactionId);
    	pentity.setAmount(amount);
    	pentity.setBuytype(buytype);
    	pentity.setCountnum(countnum);
    	pentity.setStatus(status);
    	webPayRecordServiceImpl.save(pentity);
    	
    	
    }

    public String userLogin( LoginUserParam query ) {
    	QueryWrapper<WebUserEntity> wrapper = new QueryWrapper<WebUserEntity>();
        wrapper.eq("openid", query.getOpenid() );
        List<WebUserEntity> list = webUserServiceImpl.list(wrapper);
        WebUserEntity bean = new WebUserEntity();
        if( list.size()<=0 ) {
        	bean.setUsername("yanyihui");
        	bean.setOpenid( query.getOpenid() );
        	bean.setMobile("13264034035");
        	bean.setCountnum(3);
        	bean.setStatus(1);
        	bean.setRefcode("asdfdghdadasf");
        	webUserServiceImpl.save(bean);
        }else {
        	bean = list.get(0);
        }
        Map<String,Object> claimsmap = new HashMap<>();
    	claimsmap.put("user", bean);
        return NewJwtUtils.generateToken( claimsmap );
    }
    
    
    
    public WebUserEntity getUserInfo( LoginUserParam query ) {
    	System.out.println(query.getOpenid() );
    	QueryWrapper<WebUserEntity> wrapper = new QueryWrapper<WebUserEntity>();
        wrapper.eq("openid", query.getOpenid() );
        List<WebUserEntity> list = webUserServiceImpl.list(wrapper);
        return list.get(0);
    }
    
    
    @Autowired
    WebUserServiceImpl        webUserServiceImpl;
    @Autowired
    WebPayRecordServiceImpl   webPayRecordServiceImpl;
    @Autowired
    WebTradeRecordServiceImpl webTradeRecordServiceImpl;
}
